package com.starwish.starwishblog.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.entity.Board;
import com.starwish.starwishblog.entity.Body;
import com.starwish.starwishblog.entity.Category;
import com.starwish.starwishblog.mapper.BoardMapper;
import com.starwish.starwishblog.mapper.CategoryMapper;
import com.starwish.starwishblog.vo.BoardVo;
import com.starwish.starwishblog.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BoardService {
    @Autowired
    BoardMapper boardMapper;

    @Autowired
    CategoryMapper categoryMapper;



    public int addBoards(Board Board) {
        return boardMapper.insert(Board);
    }

    public int removeBoard(Integer id) {
        return boardMapper.deleteById(id);
    }


    public int hotBoardById(Integer id,Integer status) {
        return boardMapper.hotById(id,status);
    }

    public int modify(Board Board) {
        return boardMapper.updateByPrimaryKey(Board);
    }

    public PageInfo<Board> queryBoardByPage(Integer now, Integer size, String name) {
        PageHelper.startPage(now,size);
        List<Board> lists = boardMapper.selectBoardByCons(name);
        return new PageInfo<>(lists);
    }

    public List<Board> findAllBoard() {
        return boardMapper.selectAll();
    }

    public List<BoardVo> findBoardAndCategory() {
        List<BoardVo> boardVos = boardMapper.selectAllBoards();
        if(boardVos != null){
            for (BoardVo boardVo : boardVos) {
                Integer bid = boardVo.getId();
                List<CategoryVo> categories = categoryMapper.selectByBoardId(bid);
                boardVo.setCategories(categories);
            }
        }
        return boardVos;
    }

    public String findBoardNameByCateId(Integer categoryId) {
        return  boardMapper.selectByCateId(categoryId);
    }
}
